package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @Name UserServiceImpl
 * @Author BaiXin
 * @Date 2022-11-03 18:19
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public User checkAndRegisterUser(String phone) {
        //根据手机号查询
        Wrapper<User> qw = Wrappers.lambdaQuery(User.class)
                .eq(phone != null, User::getPhone, phone);
        User user = userMapper.selectOne(qw);

        //判断
        if (user == null) {
            //没有用户，注册
            User user1 = new User();
            user1.setPhone(phone);
            user1.setStatus(1);
            userMapper.insert(user1);
            return user1;
        }
        return user;
    }
}